﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DesignLife
{

    public partial class NewIntroduction : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public DataTable News;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCookie();
                BindData();
            }
        }


        private void BindCookie()
        {
            var httpCookie = Response.Cookies["ReturnURL"];
            if (httpCookie != null)
                httpCookie.Value = "NewsIntroduction.aspx";
        }


        private void BindData()
        {
            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
                cmd.Connection = conn;
                cmd.CommandText = "select top 5 * from News ORDER BY DateTime DESC";
                cmd.CommandType = CommandType.Text;
                da.SelectCommand = cmd;
                da.Fill(ds, "News");
                News = ds.Tables["News"];

                var tbNews = new DataTable();
                tbNews.Columns.Add("NewsID", typeof(int));
                tbNews.Columns.Add("Title", typeof(string));
                tbNews.Columns.Add("Img1", typeof(string));
                tbNews.Columns.Add("Contents", typeof(string));
                tbNews.Columns.Add("DatePost", typeof(DateTime));

                if (News != null)
                {
                    for (int i = 0; i < News.Rows.Count; i++)
                    {
                        var delHtmlTitle = Regex.Replace(News.Rows[i]["Title"].ToString(), "<.*?>", string.Empty);
                        var trimTitle = SplitString(delHtmlTitle, 40);
                        var delHtmlContent = Regex.Replace(News.Rows[i]["Contents"].ToString(), "<.*?>", string.Empty);
                        var trimContent = SplitString(delHtmlContent, 300);
                        tbNews.Rows.Add(Convert.ToInt32(News.Rows[i]["NewsID"].ToString()), trimTitle, News.Rows[i]["Img1"].ToString(), trimContent, string.Format("{0:MM/dd/yyyy}", News.Rows[i]["DateTime"]));
                    }
                }

                dtlNewintro.DataSource = tbNews;
                dtlNewintro.DataBind();

                Session["News"] = tbNews;
            }
            catch
            {

            }
        }

        private object SplitString(string sText, int ilengh)
        {
            var str = "";
            var strResult = "";
            var sArray = sText.Split(' ');
            if (sArray.Length > 0)
            {
                str = sArray[0];
                var i = 1;
                while (str.Length < ilengh && i < sArray.Length)
                {
                    str += " " + sArray[i];
                    i++;
                }
            }
            if (str.Length > ilengh)
            {
                strResult = str + "...";
            }
            else
            {
                strResult = str;
            }
            return strResult;
        }

        protected void dtlNewintro_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}